﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter12.Code06
{
    using System.Collections;

    public class ShortList<T> : IList<T>
    {
        protected IList<T> innerCollection;
        protected int maxSize = 10;

        public ShortList()
            : this(10)
        {

        }

        public ShortList(int size)
        {
            maxSize = size;
            innerCollection = new List<T>();
        }

        public ShortList(IEnumerable<T> list)
            : this(10)
        {

        }

        public ShortList(int size, IEnumerable<T> list)
        {
            maxSize = size;
            innerCollection = new List<T>(list);
            if (Count > maxSize)
            {
                ThorwTooManyItemsException();
            }
        }

        protected void ThorwTooManyItemsException()
        {
            throw new IndexOutOfRangeException("Unable to add any more items, maximum size is " + maxSize.ToString() + " items.");
        }

        #region IList<T> Members

        public T this[int index]
        {
            get { return innerCollection[index]; }
            set { innerCollection[index] = value; }

        }

        public int IndexOf(T item)
        {
            return innerCollection.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            if (Count < maxSize)
            {
                innerCollection.Insert(index, item);
            }
            else
            {
                ThorwTooManyItemsException();
            }
        }

        public void RemoveAt(int index)
        {
            innerCollection.RemoveAt(index);
        }

        #endregion

        #region ICollection<T> Members

        public int Count
        {
            get { return innerCollection.Count; }
        }

        public bool IsReadOnly
        {
            get { return innerCollection.IsReadOnly; }
        }

        public void Add(T item)
        {
            if (Count < maxSize)
            {
                innerCollection.Add(item);
            }
            else
            {
                ThorwTooManyItemsException();
            }
        }

        public void Clear()
        {
            innerCollection.Clear();
        }

        public bool Contains(T item)
        {
            return innerCollection.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            innerCollection.CopyTo(array, arrayIndex);
        }

        public bool Remove(T item)
        {
            return innerCollection.Remove(item);
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return innerCollection.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}
